# -*- coding: utf-8 -*-#

# -------------------------------------------------------------------------------
# Name:         mongodb_utils
# Description:  操作mongodb
# Author:       zhuchunxu
# Date:         2020/11/4
# -------------------------------------------------------------------------------
import pymongo

from spider_source.config import MONGODB_URI, MONGODB_DBNAME, MONGODB_DBCOL


class MongodbUtils:
    def __init__(self):
        """
        初始化连接
        """
        client = pymongo.MongoClient(MONGODB_URI)
        # jobs:数据库
        jobs_db = client[MONGODB_DBNAME]
        # job_info：里面的集合
        self.job_info_col = jobs_db[MONGODB_DBCOL]

    def insert_many(self, job_info_list):
        self.job_info_col.insert_many(job_info_list)

    def insert(self, job):
        print("完成页面：{} 的内容采集！".format(job["job_url"]))
        dic_job = dict(job)
        self.job_info_col.insert(dic_job)
